﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-09-15 16:11
    /// 描 述：业务类
    /// </summary>
    public class StoreGoodsProductBLL
    {
        private StoreGoodsProductService storeGoodsProductService = new StoreGoodsProductService();

        #region 获取数据
        public async Task<TData<List<StoreGoodsProductEntity>>> GetList(StoreGoodsProductListParam param)
        {
            TData<List<StoreGoodsProductEntity>> obj = new TData<List<StoreGoodsProductEntity>>();
            obj.Data = await storeGoodsProductService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<StoreGoodsProductEntity>>> GetPageList(StoreGoodsProductListParam param, Pagination pagination)
        {
            TData<List<StoreGoodsProductEntity>> obj = new TData<List<StoreGoodsProductEntity>>();
            obj.Data = await storeGoodsProductService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<StoreGoodsProductEntity>> GetEntity(long id)
        {
            TData<StoreGoodsProductEntity> obj = new TData<StoreGoodsProductEntity>();
            obj.Data = await storeGoodsProductService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        public TData<List<goodscar>> GetSelectCarListJson(StoreGoodsProductListParam param)
        {
            TData<List<goodscar>> obj = new TData<List<goodscar>>();
            obj.Data = storeGoodsProductService.GetSelectCarListJson(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(StoreGoodsProductEntity entity)
        {
            TData<string> obj = new TData<string>();

            if (entity.Id.IsNullOrZero())
            {
                //增加颜色的库存
                var color = storeGoodsProductService.BaseRepository().FindList<StoreGoodsAttrEntity>(t => t.Id == entity.colorid).Result.FirstOrDefault();
                color.count = color.count + 1;
                await storeGoodsProductService.BaseRepository().Update(color);
            }

            await storeGoodsProductService.SaveForm(entity);

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await storeGoodsProductService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
